package com.example.service.impl;

import com.example.entity.training.TrainingPlan;
import com.example.entity.training.TrainingPlanDetail;
import com.example.entity.training.TrainingPlanExt;
import com.example.mapp.TrainingPlanDetailMapp;
import com.example.mapp.TrainingPlanMapp;
import com.example.service.TrainingPlanService;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TrainingPlanServiceImpl implements TrainingPlanService {

    private final static Logger logger = LoggerFactory.getLogger(TrainingPlanServiceImpl.class);
    @Autowired
    TrainingPlanMapp trainingPlanMapp;

    @Autowired
    TrainingPlanDetailMapp trainingPlanDetailMapp;
    @Override
    public Map<String, Object> query(TrainingPlan trainingPlan) {
        Map<String,Object> resultMap = new HashMap<>();
        try{
            if((trainingPlan.getCurrentPage()+"") != null && (trainingPlan.getPageSize()+"") != null){
                Page<TrainingPlan> page = PageHelper.startPage(trainingPlan.getCurrentPage(),trainingPlan.getPageSize());
                List<TrainingPlan> list = trainingPlanMapp.query(trainingPlan);
                resultMap.put("list",list);
                resultMap.put("total",page.getTotal());
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",resultMap);
    }

    /**
     * 新增培训计划
     * @param trainingPlan
     * @return
     */
    @Transactional
    @Override
    public Map<String, Object> add(TrainingPlan trainingPlan) {
        try{
            int returnInt = trainingPlanMapp.insert(trainingPlan);
            if(returnInt < 0){
                return ResultUtil.returnError("新增失败",null);
            }

            List<TrainingPlanExt> trainingPlanExts= new ArrayList<>();

            // 市局
            if (StringUtils.isNotBlank(trainingPlan.getPartyLevel())
                    && "1".equals(trainingPlan.getPartyLevel())) {
                trainingPlanExts = trainingPlanMapp.queryPartyAndUser(trainingPlan);
            }
            // 区局
            if (StringUtils.isNotBlank(trainingPlan.getPartyLevel())
                    && "2".equals(trainingPlan.getPartyLevel())) {
                trainingPlanExts = trainingPlanMapp.queryPartyByTwo(trainingPlan);
            }
            // 分局
            if (StringUtils.isNotBlank(trainingPlan.getPartyLevel())
                    && "3".equals(trainingPlan.getPartyLevel())) {
                trainingPlanExts = trainingPlanMapp.queryPartyByThree(trainingPlan);
            }
            // List<TrainingPlanExt> trainingPlanExts = trainingPlanMapp.queryPartyAndUser(trainingPlan);
            if(trainingPlanExts != null && trainingPlanExts.size() > 0){
                for(TrainingPlanExt trainingPlanExt : trainingPlanExts){
                    TrainingPlanDetail trainingPlanDetail = new TrainingPlanDetail();
                    trainingPlanDetail.setPartyId(trainingPlanExt.getPartyId());
                    trainingPlanDetail.setPartyName(trainingPlanExt.getPartyName());
                    trainingPlanDetail.setUnitManageUser(trainingPlanExt.getUserName());
                    trainingPlanDetail.setPartyType(trainingPlan.getPlanType());
                    trainingPlanDetail.setLeadUnit(trainingPlan.getLeadUnitName());
                    trainingPlanDetail.setStartDate(trainingPlan.getStartDate());
                    trainingPlanDetail.setEndDate(trainingPlan.getEndDate());
                    trainingPlanDetail.setPlanId(trainingPlan.getId());
                    trainingPlanDetail.setPlanName(trainingPlan.getPlanName());
                    trainingPlanDetail.setPlanStatus("0");
                    trainingPlanDetailMapp.insert(trainingPlanDetail);
                }
            }
            return ResultUtil.returnSuccess("新增成功", null);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("新增失败",null);
        }
    }

    @Override
    public Map<String, Object> update(Map map) {
        try{
           int resultInt = trainingPlanMapp.update(map);
           if(resultInt >= 0 ){
               return ResultUtil.returnSuccess("修改成功", null);
           }else{
               return ResultUtil.returnError("修改失败", null);
           }
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("修改失败", null);
        }
    }

    @Override
    public Map<String, Object> delete(Map map) {
        try{
           if(map.get("ids") != null && !"".equals(map.get("ids"))){
               for(Object id : (List)map.get("ids")){
                   trainingPlanMapp.delete(id+"");
                   TrainingPlanDetail trainingPlanDetail = new TrainingPlanDetail();
                   trainingPlanDetail.setPlanId(Long.parseLong(id + ""));
                   List<TrainingPlanDetail> result = trainingPlanDetailMapp.query(trainingPlanDetail);
                   if(CollectionUtils.isEmpty(result)){
                       continue;
                   }
                   for(TrainingPlanDetail td : result){
                       trainingPlanDetailMapp.delete(td.getId()+"");
                   }
               }
           }
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("删除失败", null);
        }
        return  ResultUtil.returnSuccess("删除成功", null);
    }


    @Override
    public Map<String, Object> uploadMaterials(TrainingPlan trainingPlan) {

        try{
            trainingPlanMapp.uploadMaterials(trainingPlan);
            return ResultUtil.returnSuccess("成功", null);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(), null);
        }
    }

    @Override
    public Map<String, Object> uploadCheckInTable(TrainingPlan trainingPlan) {
        try{
            trainingPlanMapp.uploadCheckInTable(trainingPlan);
            return ResultUtil.returnSuccess("成功", null);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(), null);
        }
    }

    @Override
    public Map<String, Object> uploadTestPaper(TrainingPlan trainingPlan) {
        try{
            trainingPlanMapp.uploadTestPaper(trainingPlan);
            return ResultUtil.returnSuccess("成功", null);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(), null);
        }
    }

    @Override
    public Map<String, Object> uploadSummaryStatement(TrainingPlan trainingPlan) {
        try{
            trainingPlanMapp.uploadSummaryStatement(trainingPlan);
            return ResultUtil.returnSuccess("成功", null);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(), null);
        }
    }
}
